package com.vincce.sport.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.Advert;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.Video;
import com.vincce.sport.mapper.AdvertMapper;
import com.vincce.sport.mapper.VideoMapper;

@Controller
@RequestMapping("/frontVideo")
public class FrontVideoController {

	@Resource
	private VideoMapper videoMapper;
	@Resource
	private AdvertMapper advertMapper;
	
	@RequestMapping("/show")
	public String show(HttpServletRequest request,Model model){
		Map<String, Object> filter = new HashMap<String, Object>();		
		Page<Advert> headAdvertPage=new Page<Advert>();
		List<Video> videoList = videoMapper.getVideoToFrontLimit9();
		model.addAttribute("videoList", replace(videoList));
		filter.put("name", "视频");
	    filter.put("status", 1);
	    headAdvertPage.setLimit(1);
	    headAdvertPage.setFilter(filter);
	    headAdvertPage.setResult(replace1(advertMapper.queryAdvertList(headAdvertPage)));
	    model.addAttribute("headAdvertPage", headAdvertPage);
	    
	    Video videoMore = videoMapper.queryVideoMore();
	    videoMore = videoMore == null ? new Video() : videoMore;
	    model.addAttribute("videoMore", videoMore);
		return "/backstage/front_videoList";
	}
	
	//转换图片路径
	private List<Video> replace(List<Video> imgTempList) {
		if(imgTempList!=null && imgTempList.size()>0){
			for(Video itl : imgTempList){
				itl.setImgUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getImgUrl(), "ori"));
			}
		}
		return imgTempList;
	}
	
	/**
	 * 图片路径替换
	 * @param imgStorageList
	 * @return
	 */
	public List<Advert> replace1(List<Advert> advertList){
		if(advertList!=null && advertList.size()>0){
			for(Advert adv:advertList){
				adv.setAdvertImg(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+adv.getAdvertImg(), "ori"));
			}
		}
		return advertList;
	}
}
